package com.eaoa.uc.busorder.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.eaoa.base.EABaseAction;
import com.eaoa.constants.AttachmentConstants;
import com.eaoa.constants.BusOrderConstants;
import com.eaoa.query.bus.BusOrderQuery;
import com.eaoa.query.pub.PubCityQuery;
import com.eaoa.uc.busorder.form.BusOrderForm;
import com.eaoa.utils.AttachmentUtils;
import com.eaoa.vo.bus.BusOrder;
import com.stony.core.util.CoreUtils;

public class BusOrderModifyPreAction extends EABaseAction {

	protected ActionForward doWork(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		BusOrderForm myform = (BusOrderForm) form;
		
		Long orderno = CoreUtils.parseLong(myform.getOrderno());

		if (orderno!=null) {
			BusOrder order = BusOrderQuery.loadByNo(orderno);
			if (!isReEntry(request)) {
				myform.setVO(order);
			}
			List cargos = new ArrayList();
			cargos.addAll(order.getBusCargos());
			myform.setBusCargosVO(cargos);
			Map attachments = AttachmentUtils.getAttachmentMap(AttachmentConstants.ATTACHMENTTYPE_ORDER,orderno);
			request.setAttribute("attachments", attachments);
		}else{
			if (!isReEntry(request)) {
//				FillCodeBO codeBO = new FillCodeBO(PubBillConstants.BILLCODE_ORDER);
//				codeBO.execute();
//			    myform.setBillcode(codeBO.getNewCode());
			}
		}
		
		Long provinceno = CoreUtils.parseLong(myform.getFromaddressprovince());
		request.setAttribute("city", PubCityQuery.getCitys(provinceno, new Long(2)));

		Long cityno = CoreUtils.parseLong(myform.getFromaddresscity());
		request.setAttribute("district", PubCityQuery.getCitys(cityno, new Long(3)));
		
		Long toprovinceno = CoreUtils.parseLong(myform.getToaddressprovince());
		request.setAttribute("tocity", PubCityQuery.getCitys(toprovinceno, new Long(2)));
		
		Long tocityno = CoreUtils.parseLong(myform.getToaddresscity());
		request.setAttribute("todistrict", PubCityQuery.getCitys(tocityno, new Long(3)));

		
		if(myform.getOrdertype()!=null){
			if(myform.getOrdertype().equals(BusOrderConstants.TYPE_LDYS)){
				return mapping.findForward("success_ldys");
			}else if(myform.getOrdertype().equals(BusOrderConstants.TYPE_THRC)){
				return mapping.findForward("success_thrc");
			}else if(myform.getOrdertype().equals(BusOrderConstants.TYPE_ZCYS)){
				return mapping.findForward("success_zcys");
			}
		}
		
		return mapping.findForward("success");

	}
}
